#!/usr/bin/bash
#
# Put customizations to your image in this file.

PATH=/opt/local/gnu/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/usr/sbin

# Munin plugins
MUNIN_PLUGINS="
mysql_bin_relay_log
mysql_commands
mysql_connections
mysql_files_tables
mysql_innodb_bpool
mysql_innodb_bpool_act
mysql_innodb_insert_buf
mysql_innodb_io
mysql_innodb_io_pend
mysql_innodb_log
mysql_innodb_rows
mysql_innodb_semaphores
mysql_innodb_tnx
mysql_myisam_indexes
mysql_network_traffic
mysql_qcache
mysql_qcache_mem
mysql_replication
mysql_select_types
mysql_slow
mysql_sorts
mysql_table_locks
mysql_tmp_tables
mysql_queries
mysql_slowqueries
mysql_threads
mysql_bytes
memcached_rates
memcached_bytes
memcached_counters
redis_connected_clients
redis_key_ratio
redis_keys_per_sec
redis_per_sec
redis_used_keys
redis_used_memory
"

# Exit if any commands fail
set -o errexit

echo "* Remove unused mysql stuff from base"
rm -rf /var/mysql/*
chown -R mysql:mysql /var/mysql

echo "* Activate munin plugins"
/opt/itime/bin/munin-node-plugins ${MUNIN_PLUGINS}

#echo "* Install nrpe redis-requirements"
#gem install redis

echo "* fix mysqltuner"
gsed -i \
     -e 's#svc:/network/mysql-quickbackup:default#svc:/pkgsrc/quickbackup-percona:default#' \
     -e 's#my $userpath = `printenv HOME`;#my $userpath = `/opt/local/heirloom/bin/printenv HOME`;#' \
     /opt/local/bin/mysqltuner.pl

mv /var/zoneinit/tmp/memcached /opt/local/etc/munin/plugin-conf.d/memcached


echo "* setup spiped"
groupadd -g 120 spiped
useradd -m -s /usr/bin/false -d / -u 120 -g spiped spiped
dd if=/dev/urandom bs=32 count=1 2>/dev/null | shasum -a 512 | awk '{print $1}' | tr -d '\n' > /etc/ssh/spiped.key
chmod 0640 /etc/ssh/spiped.key
chown root:spiped /etc/ssh/spiped.key

svccfg import /opt/local/lib/svc/manifest/spiped-memcached.xml
svccfg import /opt/local/lib/svc/manifest/spiped-percona.xml
svccfg import /opt/local/lib/svc/manifest/spiped-redis.xml
svccfg delete svc:/pkgsrc/spiped:default

echo "* setup backup path"
mkdir -p /var/backups/mysql
chmod 0750 /var/backups/percona

echo "* Cleaning up"
rm /root/customize
cp /etc/skel/.bashrc /root/.bashrc

sm-prepare-image -y
